// 导入数据库操作模块
const db = require('../db/index')


// 展示投喂广场
exports.showFeededDog = (req, res) => {

    const sql = `SELECT * FROM feeded_dog`

    db.query(sql, function (err, results) {
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "查询出错", message: err.message })
        }

        return res.send(results)

    })

}

// 通过ID查询投喂狗狗信息
exports.queryFeededDogById = (req, res) => {
    const feededId = req.body.feededId;
    console.log(req.body);

    const sql = `select * from feeded_dog where id=? `

    db.query(sql, [feededId], function (err, results) {
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "查询出错", message: err.message })
        }

        return res.send(results)

    })
}

// 通过ID修改投喂狗狗信息
exports.updateFeededDogById = (req, res) => {
    const data = req.body;
    const id = data.feededId;
    const name = data.name;
    const img = JSON.parse(data.img);
    const sex = data.sex;
    const age = data.age;
    const weight = data.weight;
    const isGrown = data.isGrown;
    const isExpelled = data.isExpelled;
    const isSterilized = data.isSterilized;
    const isVaccinated = data.isVaccinated;
    const story = data.story;

    const sql = `UPDATE feeded_dog 
                 SET name = ?,img = ?,sex = ?,age = ?,weight = ?,isGrown = ?,
                 isExpelled = ?,isSterilized = ?,isVaccinated = ?,story = ?
                 WHERE id=?; `

    const sqlStr2 = `insert into feeded_dog_img(id,feededdog_id,img) values (?,?,?)`;

    db.query(sql, [name, img[0], sex, age, weight, isGrown, isExpelled, isSterilized, isVaccinated, story, id], function (err, results) {
        if (err) {
            return res.send({ status: "修改出错", message: err.message })
        } else {
            for (let i = 0; i < img.length; i++) {
                const id2 = "feededdogimg" + new Date().getTime() + i;
                db.query(sqlStr2, [id2, id, img[i]], function (err, results) {
                    if (err) {
                        return res.send({ status: "添加图片失败", message: err.message })
                    }
                })
            }
        }
    })

    return res.send({ status: "修改成功" });

}

// 通过id删除投喂狗狗信息
exports.deleteFeededDogById = (req, res) => {
    const feededId = req.body.feededId;
    console.log(req.body);

    const sql = `delete from feeded_dog
                 where id = ?; `

    db.query(sql, [feededId], function (err, results) {
        console.log("id删除投喂狗：", results)
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "删除出错", message: err.message })
        }

        return res.send({ status: "删除成功", message: results })

    })
}

// 通过name查询投喂狗狗信息
exports.queryFeededDogByName = (req, res) => {
    const feededName = req.body.feededName;
    console.log(req.body);

    const sql = `select * from feeded_dog where name=? `

    db.query(sql, [feededName], function (err, results) {
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "查询出错", message: err.message })
        }

        return res.send(results)

    })
}


// 记录投喂金额
exports.addFeededAmount = (req, res) => {
    const data = req.body;
    const id = 'feeding' + new Date().getTime();
    const feededdog_id = data.feededdog_id;
    const user_id = data.user_id;
    const feed_amount = data.feed_amount;
    console.log(req.body);

    const sqlStr = `INSERT INTO feeding(id,feededdog_id,user_id,feed_amount) 
                    VALUES (?,?,?,?)`;

    db.query(sqlStr, [id, feededdog_id, user_id, feed_amount], function (err, results) {
        if (err) {
            return res.send({ status: "投喂失败", message: err.message })
        }
        return res.send({ status: "投喂成功" });

    })
}

// 通过userID查询我的投喂记录
exports.queryMyFeededListByUserId = (req, res) => {
    const userId = req.body.userId;
    console.log(req.body);
    const sql = `select feeded_dog.id,name,img,sex,age,weight,isGrown,isExpelled,isSterilized,isVaccinated,story,feed_time,feed_amount
    from feeded_dog,feeding
    where feeded_dog.id=feeding.feededdog_id
    and feeding.user_id= ? `

    db.query(sql, [userId], function (err, results) {
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "查询出错", message: err.message })
        }

        return res.send(results)

    })
}

// 发布投喂宠物信息
exports.addFeededDogInfo = (req, res) => {
    const data = req.body;
    const id = "feededdog" + new Date().getTime();
    const name = data.name;
    const img = JSON.parse(data.img);
    const sex = data.sex;
    const age = data.age;
    const weight = data.weight;
    const isGrown = data.isGrown;
    const isExpelled = data.isExpelled;
    const isSterilized = data.isSterilized;
    const isVaccinated = data.isVaccinated;
    const story = data.story;
    console.log(req.body);

    const sqlStr = `insert into feeded_dog(id,name,img,sex,age,weight,isGrown,isExpelled,isSterilized,isVaccinated,story) 
                    values (?,?,?,?,?,?,?,?,?,?,?)`;
    const sqlStr2 = `insert into feeded_dog_img(id,feededdog_id,img) values (?,?,?)`;
    db.query(sqlStr, [id, name, img[0], sex, age, weight, isGrown, isExpelled, isSterilized, isVaccinated, story], function (err, results) {
        if (err) {
            return res.send({ status: "发布失败", message: err.message })
        } else {
            for (let i = 0; i < img.length; i++) {
                const id2 = "feededdogimg" + new Date().getTime() + i;
                db.query(sqlStr2, [id2, id, img[i]], function (err, results) {
                    if (err) {
                        return res.send({ status: "发布失败", message: err.message })
                    }
                })
            }
        }
    })
    return res.send({ status: "发布成功" });
}


// 通过投喂宠物id获取图片
exports.showFeededDogImg = (req, res) => {
    const feededdog_id = req.body.feededdog_id;

    const sql = `SELECT *
                FROM feeded_dog_img
                WHERE feededdog_id = ?`

    db.query(sql, [feededdog_id], function (err, results) {
        // 执行 SQL 语句失败
        if (err) {
            return res.send({ status: "无法获取图片信息", message: err.message });
        }
        return res.send(results);

    })

}
